package com.example.weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.weather.controller.AppController;

public class MainActivity extends Activity {
	private String urlJsonArray = "http://api.androidhive.info/volley/person_array.json";
	private Button bt_arr, bt_obj;
	private TextView array, object;
	private ProgressDialog pDialog;
	private String jsonResponse;
	private static String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		pDialog();
		bt_arr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				makeJsonArrayRequest();
			}

		});
		bt_obj.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				makeJsonObject();
			}

		});
	}

	private void pDialog() {
		pDialog = new ProgressDialog(MainActivity.this);
		pDialog.setMessage("Please wait..");
		pDialog.setCancelable(false);
	}

	private void initUI() {
		bt_arr = (Button) findViewById(R.id.bt_arr);
		bt_obj = (Button) findViewById(R.id.bt_obj);
		array = (TextView) findViewById(R.id.array);
		object = (TextView) findViewById(R.id.object);
	}

	private void makeJsonArrayRequest() {

		showDialog();

		JsonArrayRequest req = new JsonArrayRequest(urlJsonArray,
				new Response.Listener<JSONArray>() {
					public void onResponse(JSONArray response) {
						Log.d(TAG, response.toString());

						try {
							// Parsing json array response
							// loop through each json object
							jsonResponse = "";
							for (int i = 0; i < response.length(); i++) {

								JSONObject person = (JSONObject) response
										.get(i);

								String name = person.getString("name");
								String email = person.getString("email");
								JSONObject phone = person
										.getJSONObject("phone");
								String home = phone.getString("home");
								String mobile = phone.getString("mobile");

								jsonResponse += "Name: " + name + "\n\n";
								jsonResponse += "Email: " + email + "\n\n";
								jsonResponse += "Home: " + home + "\n\n";
								jsonResponse += "Mobile: " + mobile + "\n\n\n";

							}

							array.setText(jsonResponse);

						} catch (JSONException e) {
							e.printStackTrace();
							Toast.makeText(getApplicationContext(),
									"Error: " + e.getMessage(),
									Toast.LENGTH_LONG).show();
						}

						hideDialog();
					}
				}, new Response.ErrorListener() {
					public void onErrorResponse(VolleyError error) {
						VolleyLog.d(TAG, "Error: " + error.getMessage());
						Toast.makeText(getApplicationContext(),
								error.getMessage(), Toast.LENGTH_SHORT).show();
						hideDialog();
					}
				});

		// Adding request to request queue
		AppController.getInstance().addToRequestQueue(req);
	}

	private void showDialog() {
		if (!pDialog.isShowing()) {
			pDialog.show();
		}
	}

	private void hideDialog() {
		if (pDialog.isShowing()) {
			pDialog.dismiss();
		}
	}

	private void makeJsonObject() {

	}
}
